package cn.wsalix.biz.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import cn.anlaser.repository.CommonRepository;
import cn.wsalix.biz.entity.Advertise;
import cn.wsalix.biz.entity.RelationAd;
import cn.wsalix.biz.type.AdTypeEnum;

public interface RelationAdRepository extends CommonRepository<RelationAd> {
	@Query("select ad from RelationAd relaAd,Advertise ad where relaAd.advertise.id=ad.id and relaAd.fkId=:fkId and relaAd.adType = :adType")
	public List<Advertise> findByTypeAndFkid(@Param("fkId") Long fkId,
			@Param("adType") AdTypeEnum adType);

	@Modifying(clearAutomatically = true)
	@Query("delete from RelationAd relaAd where relaAd.advertise.id =:adId")
	public void deleteByAdId(@Param("adId") Long adId);

	@Modifying(clearAutomatically = true)
	@Query("delete from RelationAd relaAd where relaAd.fkId=:fkId and relaAd.adType = :adType")
	public void deleteByAdTypeAndFkid(@Param("fkId") Long fkId,
			@Param("adType") int adType);

	@Modifying(clearAutomatically = true)
	@Query("delete from RelationAd relaAd where relaAd.fkId=:fkId and relaAd.advertise.id=:adId and relaAd.adType = :adType")
	public void deleteByAdTypeAndFkid(@Param("fkId") Long fkId,
			@Param("adId") Long adId, @Param("adType") AdTypeEnum adType);

}
